package com.iking.toolkit.formatdeal.controller;

import com.iking.toolkit.formatdeal.enums.FileFormatPathEnum;
import com.iking.toolkit.formatdeal.model.FileFormatConversion;
import com.iking.toolkit.formatdeal.model.dto.SelectByPageDTO;
import com.iking.toolkit.formatdeal.service.FileFormatConversionService;
import com.iking.toolkit.model.Msg;
import com.iking.toolkit.model.PageRes;
import com.iking.toolkit.util.FileFormatUtil;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import jakarta.validation.constraints.NotBlank;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

/**
 * 功能描述:文件格式转换任务管理
 *
 * @author: yfl
 * @date: 2025/8/7 09:18
 */
@Slf4j
@Tag(name = "FileFormatConversionController", description = "文件格式转换任务管理")
@RequestMapping("/fileFormatConversion")
@RestController
@Validated
public class FileFormatConversionController {


    @Autowired
    private FileFormatConversionService fileFormatConversionService;

    @Operation(summary = "文件格式转换,上传文件")
    @PostMapping(value = "/fileFormat", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    public Msg fileFormat(@RequestParam("file") MultipartFile multipartFile, @RequestParam("uploadId") String uploadId, FileFormatPathEnum fileFormatPathEnum) {
        if (multipartFile.isEmpty()) {
            return Msg.fail("上传的待转换文件为空，请重新上传!");
        }
        String contentType = multipartFile.getContentType();
        // 获取文件名和扩展名
        String fileName = multipartFile.getOriginalFilename();
        String fileSuffix = FileFormatUtil.getFileSuffix(fileName);
        if (!fileFormatPathEnum.getFileSuffix().contains(fileSuffix)) {
            log.warn("转换文件类型错误，文件名: {}, 后缀: {}, Content-Type: {}", fileName, fileSuffix, contentType);
            return Msg.fail("请上传" + fileFormatPathEnum.getFileSuffix() + "类型文件!");
        }
        // 执行格式转换
        return fileFormatConversionService.fileFormatConversion(multipartFile, uploadId, fileFormatPathEnum);
    }

    @Operation(summary = "根据业务id查询文件格式转换记录")
    @GetMapping("/addFileFormatConversion")
    public Msg<FileFormatConversion> selectByBusinessId(@RequestParam @NotBlank(message = "业务Id不能为空") String businessId) {
        return fileFormatConversionService.selectByBusinessId(businessId);
    }

    @Operation(summary = "分页查询文件格式转换记录")
    @PostMapping("/selectByPage")
    public Msg<PageRes<FileFormatConversion>> selectByPage(@RequestBody @Valid SelectByPageDTO selectByPageDTO) {
        return fileFormatConversionService.selectByPage(selectByPageDTO);
    }


}
